package com.learn.shop.controller;


import com.github.pagehelper.PageInfo;
import com.learn.shop.constant.NovelContent;
import com.learn.shop.constant.R;
import com.learn.shop.mapper.SDictDataMapper;
import com.learn.shop.pojo.entriy.SDictData;
import com.learn.shop.pojo.vo.SDictDataVo;
import com.learn.shop.service.SDictDataService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("dict")
@Api(tags = "字典管理")
public class DictController {

    @Autowired
    private SDictDataService sDictDataService;

    @Autowired
    private SDictDataMapper sDictDataMapper;

    @ApiOperation("字典列表")
    @PostMapping("list")
    public R<PageInfo<SDictData>> list(@RequestBody SDictDataVo vo) {
        PageInfo<SDictData> list = sDictDataService.list(vo);
        return R.ok(list);
    }

    /**
     * 获取小说所有类型
     *
     * @return 结果
     */
    @GetMapping("/getNovelType")
    @ApiOperation("获取小说所有类型")
    public R getNovelTypeToDict() {
        List<SDictData> list = sDictDataMapper.list(NovelContent.novel_type);
        return R.ok(list);
    }

    @PostMapping("/editDict")
    @ApiOperation("修改字典")
    public R editDict(@RequestBody SDictData sDictData) {
        try {
            sDictDataService.update(sDictData);
            return R.ok("修改成功!");
        } catch (Exception e) {
            e.printStackTrace();
            return R.fail("修改失败!");
        }
    }

    @GetMapping("/deleteDict/{id}")
    @ApiOperation("删除字典")
    public R deleteDict(@PathVariable Integer id) {
        try {
            sDictDataService.deleteById(id);
            return R.ok("删除成功!");
        } catch (Exception e) {
            e.printStackTrace();
            return R.fail("删除失败!");
        }
    }

    @GetMapping("/selectById/{id}")
    @ApiOperation("查询一个")
    public R selectById(@PathVariable Integer id) {
        SDictData sDictData = sDictDataService.queryById(id);
        return R.ok(sDictData);
    }
}
